﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SwitchMiner.Interfaces;
using SwitchMiner.Models;

namespace SwitchMiner.Controlers
{
    public class ParametersControler
    {
        private List<ParameterModel> _Parameters = null;
        private IModelNotification _newMinerCallback = null;
        public ParametersControler(IModelNotification minercallback, List<ParameterModel> parametersModel)
        {
            _Parameters     = parametersModel;
            _newMinerCallback = minercallback;
            if (_Parameters.Count > 0 && _newMinerCallback != null)
            {
                _newMinerCallback.ParametersModelsChanged(_Parameters);
            }
        }

        public List<ParameterModel> GetModel()
        {
            return _Parameters;
        }

        public void AddParameter(ParameterModel model)
        {
            if (_Parameters != null)
            {
                if (_Parameters.Contains(model) == false)
                {
                    foreach (ParameterModel innerModel in _Parameters)
                    {
                        if (innerModel.Name == model.Name)
                        {
                            return;
                        }
                    }
                    _Parameters.Add(model);
                    if (null != _newMinerCallback)
                    {
                        _newMinerCallback.ParametersModelsChanged(_Parameters);
                    }
                }
            }
        }
        public void RemoveParameter(ParameterModel model)
        {
            if (_Parameters != null)
            {
                if (_Parameters.Contains(model) == true)
                {
                    _Parameters.Remove(model);
                    if (null != _newMinerCallback)
                    {
                        _newMinerCallback.ParametersModelsChanged(_Parameters);
                    }
                }
            }
        }

        public ParameterModel GetModelFromName(string name)
        {
            foreach (ParameterModel model in _Parameters)
            {
                if (model.Name == name)
                {
                    return model;
                }
            }
            return null;
        }
    }
}
